<?php
/**
 * @file   common.php
 * @date   2017-11-15 15:22:57
 * @author huwhis<huuwhois>
 * @version   0.0.1
 */

// 应用公共文件

/**2017-11-15
 * inet_ntoa()
 * 将数据库取出来的ip整数形转为ip地址
 * 参数说明 $dd=256;第一次对256取余活的ip地址第4个,第二次活的第三个,依次类推
 */
function inet_ntoa($ip = '0')
{
    $array=array();
    $dd=256;
    for ($i=4; $i>0; $i--) {
        $array[$i] = $ip%$dd;
        $ip = intval(floor($ip/$dd));
    }
    return $array[1].'.'.$array[2].'.'.$array[3].'.'.$array[4];
}



/**2017-12-4
* set_stmp()
* 配置发送邮件的邮箱服务smtp.163.com::huwhereis@163.com
*/
function send_email($smtpemailto = "", $emailsubject = "", $emailbody = "")
{
    $smtpserver = "smtp.163.com"; //SMTP服务器
    $smtpserverport = 25; //SMTP服务器端口
    $smtpusermail = "huwhereis@163.com"; //SMTP服务器的用户邮箱
    $smtpuser = "huwhereis@163.com"; //SMTP服务器的用户帐号
    $smtppass = "humail001"; //SMTP服务器的用户密码
    $smtp = new \mylib\Smtp($smtpserver, $smtpserverport, true, $smtpuser, $smtppass); //这里面的一个true是表示使用身份验证,否则不使用身份验证.
    $emailtype = "HTML"; //信件类型，文本:text；网页：HTML
    $smtpemailfrom = $smtpusermail;

    $result = $smtp->sendMail($smtpemailto, $smtpemailfrom, $emailsubject, $emailbody, $emailtype);
    return $result;
}



/*无限级分类*/
function get_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root = 0)
{
    $tree     = array();
    $packData = array();
    foreach ($list as $data) {
//外层数组便脚标
        $packData[$data[$pk]] = $data;
    }
    foreach ($packData as $key => $val) {
        if ($val[$pid] == $root) {
//代表跟节点, 重点一
            $tree[] = &$packData[$key];
        } else {
//找到其父类,重点二
            $packData[$val[$pid]][$child][] = &$packData[$key];
        }
    }
    return $tree;
}

/*缩略图*/
function get_thumb($file, $width = 150, $height = 150)
{
//打开原图    
    $image = \think\Image::open($file);
//生成缩略图
    $fileinfo= pathinfo($file);
// var_dump($fileinfo);
/*==>
*array (size=4)
*   'dirname' => string 'uploads/20171205' (length=16)
*   'basename' => string 'c718905b8b0afcf3eda4f7b0efa5456f.png' (length=36)
*   'extension' => string 'png' (length=3)
*   'filename' => string 'c718905b8b0afcf3eda4f7b0efa5456f' (length=32)
*/
    if (!is_dir($fileinfo['dirname'].DS.'thumb')) {
        mkdir($fileinfo['dirname'].DS.'thumb', 0777, true);
    }
    $thumbname = $fileinfo['dirname'].DS.'thumb'.DS.$fileinfo['filename'].'_thumb.'.$fileinfo['extension'];
// echo $thumbname;exit;
    $test=$image->thumb($width, $height, \think\Image::THUMB_FIXED)->save($thumbname);
    if ($test) {
        return $thumbname;
    } else {
        return 0;
    }
}

//获取浏览器信息
function browse_info()
{
    if (!empty($_SERVER['HTTP_USER_AGENT'])) {
        $br = $_SERVER['HTTP_USER_AGENT'];
        if (preg_match('/MSIE/i', $br)) {
            $br = 'MSIE';
        } elseif (preg_match('/Firefox/i', $br)) {
            $br = 'Firefox';
        } elseif (preg_match('/Chrome/i', $br)) {
            $br = 'Chrome';
        } elseif (preg_match('/Safari/i', $br)) {
            $br = 'Safari';
        } elseif (preg_match('/Opera/i', $br)) {
            $br = 'Opera';
        } else {
            $br = 'Other';
        }
        return $br;
    } else {
        return 'unknow';
    }
}

//获取操作系统
function get_os()
{
    if (!empty($_SERVER['HTTP_USER_AGENT'])) {
        $os = $_SERVER['HTTP_USER_AGENT'];
        if (preg_match('/win/i', $os)) {
            $os = 'Windows';
        } elseif (preg_match('/mac/i', $os)) {
            $os = 'MAC';
        } elseif (preg_match('/linux/i', $os)) {
            $os = 'Linux';
        } elseif (preg_match('/unix/i', $os)) {
            $os = 'Unix';
        } elseif (preg_match('/bsd/i', $os)) {
            $os = 'BSD';
        } else {
            $os = 'Other';
        }
        return $os;
    } else {
        return 'unknow';
    }
}

//random() 函数返回随机整数
function random($length = 6, $numeric = 0)
{
    PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
    if ($numeric) {
        $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
    } else {
        $hash = '';
        $chars = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz';
        $max = strlen($chars) - 1;
        for ($i = 0; $i < $length; $i++) {
            $hash .= $chars[mt_rand(0, $max)];
        }
    }
    return $hash;
}

//将 xml数据转换为数组格式。
function xmlToArray($xml)
{
    $reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
    if (preg_match_all($reg, $xml, $matches)) {
        $count = count($matches[0]);
        for ($i = 0; $i < $count; $i++) {
            $subxml= $matches[2][$i];
            $key = $matches[1][$i];
            if (preg_match($reg, $subxml)) {
                $arr[$key] = xml_to_array($subxml);
            } else {
                $arr[$key] = $subxml;
            }
        }
    }
    return $arr;
}
